<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
    <title>PHP Class Structure Documentation</title>
    <link href="css/reset.css" media="" rel="stylesheet" type="text/css" />
    <link href="css/styles.css" media="screen" rel="stylesheet" type="text/css" />
    <link href="css/datatable.css" media="screen" rel="stylesheet" type="text/css" />
</head>
<body style="padding: 1em 2em;">
    <?php
    ini_set('display_errors', true);
    error_reporting(E_ALL);
    
    require_once dirname(__FILE__) . '/../examples/_include.php';
    
    //==========================================================================
    
    $str_table_source = 'test';
    
    $mix_sorting = 'value';
    $mix_grouping = array('group_id' => 'group_name');
    $mix_render = array(
        'offset' => null,
        'size' => null
    );
    
    $arr_summaries = array();
    $obj_summary = new Common_DataTable_Summary_Sum();
    $obj_summary->setStaticValues(array(
        'name' => '(__COUNT__) Items'
    ));
    $obj_summary->setStaticValuesForGroup(array(    
        'name' => '(__COUNT__) Items for __GROUP__'
    ));
    $arr_summaries[] = $obj_summary;
    
    $obj_summary = new Common_DataTable_Summary_Custom(array(
        'name' => 'Custom Group __GROUP__'
    ), 0
    );
    
    $obj_summary->setPositionType(Common_DataTable_Summary::POSITION_HEADER);
    $arr_summaries[] = $obj_summary;
    
    $arr_footers = array(
        array(
            'name' => 'Custom Footer'
        )
    );
    
    $arr_group_custom = array(
        2 => array(
            'id' => 'Hello',
            'name' => 'Custom Group __GROUP__'
        )
    );
    
    require_once dirname(__FILE__) . '/datatable.include.php';
    
    $arr_rendered = array();
    
    //--------------------------------------------------------------------------
    
    
    /*
    $flt_start = microtime(true);
    
    $obj = new Common_DataTable();
    $obj->setCaption('Table with a CSV Source');
    $obj->setHeaders($arr_headers);
    $obj->bindData(dirname(__FILE__).'/../data/sample.csv', array(
        'number_of_headers' => 1
    ));
    
    if($mix_sorting) {
        $obj->sortDataBy($mix_sorting);
    }
    
    if($mix_grouping) {
        $obj->groupDataBy('group_name', $arr_group_custom);
    }
    
    if($arr_summaries) {
        foreach($arr_summaries as $obj_summary) {
            $obj->addDataSummary($obj_summary);
        }
    }
    
    $obj->setFooters($arr_footers);
   
    $str_render = $obj->render($mix_render); 
    
    $flt_end = microtime(true);
    
    $arr_rendered[] = array(
        'time' => $flt_end - $flt_start,
        'echo' =>  $str_render     
    );
    */
    
    //--------------------------------------------------------------------------
    
    $flt_start = microtime(true);
    
    $obj = new Common_DataTable();
    $obj->setCaption('Table with an Array Source');
    $obj->setHeaders($arr_headers);
    $obj->bindData($arr_data);
    
    if($mix_sorting) {
        $obj->sortDataBy($mix_sorting);
    }
    
    if($mix_grouping) {
        $obj->groupDataBy($mix_grouping, $arr_group_custom);
    }
    
    if($arr_summaries) {
        foreach($arr_summaries as $obj_summary) {
            $obj->addDataSummary($obj_summary);
        }
    }
    
    $obj->setFooters($arr_footers);
   
    $obj_persistence = Common_DataTable_Persistence_Factory::create(Common_DataTable_Persistence_Factory::STRATEGY_FILE, $obj, array(
        'persistence_directory' => '/tmp/persistence-data'
    ));
    $str_key = $obj_persistence->store();
    $obj = $obj_persistence->retrieve($str_key);
    
    
    $str_render = $obj->render($mix_render); 
    
    $flt_end = microtime(true);
    
    $arr_rendered[] = array(
        'time' => $flt_end - $flt_start,
        'echo' =>  $str_render     
    );
    
    
    /*
    
    
    $obj_exporter = new Common_DataTable_Exporter($obj);
    $str_export = $obj_exporter->export('csv');
    egg( $str_export);
    die;
    
    //--------------------------------------------------------------------------
    
    $flt_start = microtime(true);
    
    $obj = new Common_DataTable();
    $obj->setCaption('Table with an Database Table Source');
    $obj->setHeaders($arr_headers);
    $obj->bindData(new Common_DataTable_Model_DatabaseTable($obj_datagateway, $str_table_source));
    
    if($mix_sorting) {
        $obj->sortDataBy($mix_sorting);
    }
    
    if($mix_grouping) {
        $obj->groupDataBy($mix_grouping, $arr_group_custom);
    }
    
    if($arr_summaries) {
        foreach($arr_summaries as $obj_summary) {
            $obj->addDataSummary($obj_summary);
        }
    }
    
    $obj->setFooters($arr_footers);

    $str_render = $obj->render($mix_render); 
    
    $flt_end = microtime(true);
    
    $arr_rendered[] = array(
        'time' => $flt_end - $flt_start,
        'echo' =>  $str_render     
    );
    */
    //--------------------------------------------------------------------------
    
    $i = 0;
    foreach($arr_rendered as $arr) {
        echo '<div style="float:left;padding:1em;">';
        echo '<div>Time: ' . $arr['time'] . '</div>';
        echo $arr['echo'];
        echo '</div>';
        
        $i++;
        
        if(($i%2) == 0) echo '<div style="clear:both;"></div>';
    }
    
    $obj_exporter = new Common_DataTable_Exporter($obj);
    $str_csv = $obj_exporter->export(Common_DataTable_Exporter::EXPORT_CSV);
    egg($str_csv);
    
    ?>
</body>
</html>
